<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>为数组的每个元素应用回调函数</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-map" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_map</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_map</span> &mdash; <span class="dc-title">
     为数组的每个元素应用回调函数
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-map-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_map</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_map()</strong></span>：返回数组，是为 <code class="parameter">array1</code> 每个元素应用 <code class="parameter">callback</code>函数之后的数组。
   <code class="parameter">callback</code> 函数形参的数量和传给 <span class="function"><strong>array_map()</strong></span> 数组数量，两者必须一样。
  </p>
   </div>

 
 <div class="refsect1 parameters" id="refsect1-function.array-map-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       回调函数，应用到每个数组里的每个元素。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       数组，遍历运行 <code class="parameter">callback</code> 函数。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       数组列表，每个都遍历运行 <code class="parameter">callback</code> 函数。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-map-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数组，包含 <code class="parameter">callback</code> 函数处理之后 <code class="parameter">array1</code>  的所有元素。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-map-examples">
  <h3 class="title">范例</h3>
  <p class="para">
     <div class="example" id="example-6041">
      <p><strong>Example #1 <span class="function"><strong>array_map()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">cube</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cube"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

      <div class="example-contents"><p>
       这使得 <var class="varname"><var class="varname">$b</var></var> 成为：
      </p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 8
    [2] =&gt; 27
    [3] =&gt; 64
    [4] =&gt; 125
)
</pre></div>
      </div>
     </div>
  </p>
  <p class="para">
   <div class="example" id="example-6042">
    <p><strong>Example #2 <span class="function"><strong>array_map()</strong></span> 使用匿名函数 (PHP 5.3.0 起)</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$func&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 2
    [1] =&gt; 4
    [2] =&gt; 6
    [3] =&gt; 8
    [4] =&gt; 10
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6043">
    <p><strong>Example #3 <span class="function"><strong>array_map()</strong></span>：使用更多的数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">show_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</span><span style="color: #DD0000">"The&nbsp;number&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #DD0000">&nbsp;is&nbsp;called&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #DD0000">&nbsp;in&nbsp;Spanish"</span><span style="color: #007700">);<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">map_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(array(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"dos"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"tres"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"show_Spanish"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"map_Spanish"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
// printout of $c
Array
(
    [0] =&gt; The number 1 is called uno in Spanish
    [1] =&gt; The number 2 is called dos in Spanish
    [2] =&gt; The number 3 is called tres in Spanish
    [3] =&gt; The number 4 is called cuatro in Spanish
    [4] =&gt; The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] =&gt; Array
        (
            [1] =&gt; uno
        )

    [1] =&gt; Array
        (
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [3] =&gt; tres
        )

    [3] =&gt; Array
        (
            [4] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [5] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   传入两个及以上的数组时，它们元素数量将会相同。因为回调函数会并行地处理相互对应的元素。
   如果几个数组的元素数量不一致：空元素会扩展短那个数组，直到长度和最长的数组一样。
  </p>
  <p class="para">
   此函数有个有趣的用法：传入 <strong><code>NULL</code></strong> 作为回调函数的名称，将创建多维数组（一个数组，内部包含数组。）
  </p>
  <p class="para">
   <div class="example" id="example-6044">
    <p><strong>Example #4 多维数组：创建数组，内部包含数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"two"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"three"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"four"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"five"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"dos"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"tres"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; one
            [2] =&gt; uno
        )

    [1] =&gt; Array
        (
            [0] =&gt; 2
            [1] =&gt; two
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [0] =&gt; 3
            [1] =&gt; three
            [2] =&gt; tres
        )

    [3] =&gt; Array
        (
            [0] =&gt; 4
            [1] =&gt; four
            [2] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [0] =&gt; 5
            [1] =&gt; five
            [2] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   如果仅传入一个数组，键（key）会保留；传入多个数组，键（key）是整型数字的序列。
  </p>
  <p class="para">
   <div class="example" id="example-6045">
    <p><strong>Example #5 <span class="function"><strong>array_map()</strong></span> 键（key）是 string</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"stringkey"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"value"</span><span style="color: #007700">);<br />function&nbsp;</span><span style="color: #0000BB">cb1</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array&nbsp;(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br />function&nbsp;</span><span style="color: #0000BB">cb2</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array&nbsp;(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb2"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [&quot;stringkey&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [&quot;stringkey&quot;]=&gt;
  string(5) &quot;value&quot;
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-map-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_filter.html" class="function" rel="rdfs-seeAlso">array_filter()</a> - 用回调函数过滤数组中的单元</span></li>
    <li class="member"><span class="function"><a href="array_reduce.html" class="function" rel="rdfs-seeAlso">array_reduce()</a> - 用回调函数迭代地将数组简化为单一的值</span></li>
    <li class="member"><span class="function"><a href="array_walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - 使用用户自定义函数对数组中的每个元素做回调处理</span></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>